package bancosys.tec.services.tools.export;

import jmine.tec.services.api.ActionsEnum;

import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.util.HSSFColor;

/**
 * @author Rafael Volpato (Jun 29, 2007)
 */
public class CellStyles {

    private HSSFColor headerColor;

    private HSSFColor actionPreReqColor;

    private HSSFColor actionIncluirColor;

    private HSSFColor actionAlterarColor;

    private HSSFColor actionExcluirColor;

    private HSSFColor actionConfirmarColor;

    private HSSFColor actionRegistrarColor;

    private HSSFColor actionLiquidarColor;

    private HSSFColor actionProcessarColor;

    private HSSFColor actionValidarColor;

    private HSSFCellStyle headerCellStyle;

    private HSSFCellStyle actionPreReqCellStyle;

    private HSSFCellStyle actionIncluirCellStyle;

    private HSSFCellStyle actionAlterarCellStyle;

    private HSSFCellStyle actionExcluirCellStyle;

    private HSSFCellStyle actionConfirmarCellStyle;

    private HSSFCellStyle actionRegistrarCellStyle;

    private HSSFCellStyle actionLiquidarCellStyle;

    private HSSFCellStyle actionProcessarCellStyle;

    private HSSFCellStyle actionValidarCellStyle;

    public CellStyles(HSSFWorkbook workbook) {
        this.setupCellColors();
        this.setupCellStyles(workbook);
    }

    private void setupCellColors() {
        this.headerColor = new HSSFColor.AUTOMATIC();
        this.actionPreReqColor = new HSSFColor.WHITE();
        this.actionIncluirColor = new HSSFColor.WHITE();
        this.actionAlterarColor = new HSSFColor.WHITE();
        this.actionExcluirColor = new HSSFColor.WHITE();
        this.actionConfirmarColor = new HSSFColor.WHITE();
        this.actionRegistrarColor = new HSSFColor.WHITE();
        this.actionLiquidarColor = new HSSFColor.WHITE();
        this.actionProcessarColor = new HSSFColor.WHITE();
        this.actionValidarColor = new HSSFColor.WHITE();
    }

    private void setupCellStyles(HSSFWorkbook workbook) {
        this.setupHeaderCellStyle(workbook);

        /* Estilos das acoes */
        this.setupActionStylePreReq(workbook);
        this.setupActionStyleIncluir(workbook);
        this.setupActionStyleAlterar(workbook);
        this.setupActionStyleExcluir(workbook);
        this.setupActionStyleConfirmar(workbook);
        this.setupActionStyleRegistrar(workbook);
        this.setupActionStyleLiquidar(workbook);
        this.setupActionStyleProcessar(workbook);
        this.setupActionStyleValidar(workbook);
    }

    private void setupHeaderCellStyle(HSSFWorkbook workbook) {
        this.headerCellStyle = workbook.createCellStyle();

        this.addBorder(this.headerCellStyle, HSSFCellStyle.BORDER_MEDIUM);
        this.headerCellStyle.setFillPattern(HSSFCellStyle.FINE_DOTS);
        this.headerCellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
        this.headerCellStyle.setFillBackgroundColor(this.headerColor.getIndex());
    }

    private HSSFCellStyle createActionCellStyle(HSSFWorkbook workbook) {
        HSSFCellStyle actionCellStyle = workbook.createCellStyle();

        this.addBorder(actionCellStyle, HSSFCellStyle.BORDER_THIN);
        // actionCellStyle.setFillPattern(HSSFCellStyle.FINE_DOTS);
        actionCellStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_TOP);

        return actionCellStyle;
    }

    private void setupActionStylePreReq(HSSFWorkbook workbook) {
        this.actionPreReqCellStyle = this.createActionCellStyle(workbook);

        this.actionPreReqCellStyle.setFillBackgroundColor(this.actionPreReqColor.getIndex());
    }

    private void setupActionStyleIncluir(HSSFWorkbook workbook) {
        this.actionIncluirCellStyle = this.createActionCellStyle(workbook);

        this.actionIncluirCellStyle.setFillBackgroundColor(this.actionIncluirColor.getIndex());
    }

    private void setupActionStyleAlterar(HSSFWorkbook workbook) {
        this.actionAlterarCellStyle = this.createActionCellStyle(workbook);

        this.actionAlterarCellStyle.setFillBackgroundColor(this.actionAlterarColor.getIndex());
    }

    private void setupActionStyleExcluir(HSSFWorkbook workbook) {
        this.actionExcluirCellStyle = this.createActionCellStyle(workbook);

        this.actionExcluirCellStyle.setFillBackgroundColor(this.actionExcluirColor.getIndex());
    }

    private void setupActionStyleConfirmar(HSSFWorkbook workbook) {
        this.actionConfirmarCellStyle = this.createActionCellStyle(workbook);

        this.actionConfirmarCellStyle.setFillBackgroundColor(this.actionConfirmarColor.getIndex());
    }

    private void setupActionStyleRegistrar(HSSFWorkbook workbook) {
        this.actionRegistrarCellStyle = this.createActionCellStyle(workbook);

        this.actionRegistrarCellStyle.setFillBackgroundColor(this.actionRegistrarColor.getIndex());
    }

    private void setupActionStyleLiquidar(HSSFWorkbook workbook) {
        this.actionLiquidarCellStyle = this.createActionCellStyle(workbook);

        this.actionLiquidarCellStyle.setFillBackgroundColor(this.actionLiquidarColor.getIndex());
    }

    private void setupActionStyleProcessar(HSSFWorkbook workbook) {
        this.actionProcessarCellStyle = this.createActionCellStyle(workbook);

        this.actionProcessarCellStyle.setFillBackgroundColor(this.actionProcessarColor.getIndex());
    }

    private void setupActionStyleValidar(HSSFWorkbook workbook) {
        this.actionValidarCellStyle = this.createActionCellStyle(workbook);

        this.actionValidarCellStyle.setFillBackgroundColor(this.actionValidarColor.getIndex());
    }

    private void addBorder(HSSFCellStyle cellStyle, short border) {
        cellStyle.setBorderBottom(border);
        cellStyle.setBorderTop(border);
        cellStyle.setBorderLeft(border);
        cellStyle.setBorderRight(border);
    }

    public HSSFCellStyle getHeaderCellStyle() {
        return this.headerCellStyle;
    }

    @SuppressWarnings("deprecation")
    public HSSFCellStyle getActionCellStyle(ActionsEnum action) {
        HSSFCellStyle actionStyle = null;

        if (action.incluir()) {
            actionStyle = this.actionIncluirCellStyle;

        } else if (action.validar()) {
            actionStyle = this.actionValidarCellStyle;

        } else if (action.liquidar()) {
            actionStyle = this.actionLiquidarCellStyle;

        } else if (action.processar()) {
            actionStyle = this.actionProcessarCellStyle;

        } else if (action.preReq()) {
            actionStyle = this.actionPreReqCellStyle;

        } else if (action.excluir()) {
            actionStyle = this.actionExcluirCellStyle;

        } else if (action.confirmar()) {
            actionStyle = this.actionConfirmarCellStyle;

        } else if (action.registrar()) {
            actionStyle = this.actionRegistrarCellStyle;

        } else if (action.alterar()) {
            actionStyle = this.actionAlterarCellStyle;
        }

        return actionStyle;
    }
}
